<?php

namespace app\models;

use Yii;
use yii\base\UserException;

/**
 * This is the model class for table "member".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $name
 * @property double $salary
 * @property double $deposit
 * @property integer $position
 * @property double $commission
 * @property boolean $status
 * @property string $create_at
 * @property string $update_at
 *
 * @property User $user
 * @property Order[] $orders
 * @property Order[] $orders0
 * @property Product[] $products
 */
class Member extends \app\models\BaseModel {
    const POSITION_CEO = 1;
    const POSITION_CDO = 2;
    const POSITION_CMO = 3;
    const POSITION_CPO = 4;
    public static $positions = [
        self::POSITION_CEO => '首席执行官',
        self::POSITION_CDO => '设计总监',
        self::POSITION_CMO => '营销总监',
        self::POSITION_CPO => '生产总监',
    ];
    public static $salaryArray = [
        self::POSITION_CEO => 10000,
        self::POSITION_CDO => 8000,
        self::POSITION_CMO => 8000,
        self::POSITION_CPO => 8000,
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'member';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'name', 'salary', 'deposit', 'position', 'commission'], 'required'],
            [['user_id', 'position'], 'integer'],
            [['status'], 'boolean'],
            [['salary', 'deposit', 'commission'], 'number'],
            [['create_at', 'update_at'], 'safe'],
            [['name'], 'string', 'max' => 255],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '所在公司',
            'name' => '成员姓名',
            'salary' => '每财年工资',
            'deposit' => '存款',
            'position' => '职位',
            'commission' => '提成',
            'status' => '\"在职\" or \"离职\"',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
        ];
    }

    public function fields() {
        $fields = [
            'id',
            'name',
            'salary',
            'deposit',
            'position',
            'commission',
            'status',
            'user',
        ];
        $fields = array_combine($fields, $fields);

        return $fields;
    }

    public function extraFields() {
        $fields = [
            'user_id',
            'create_at',
            'update_at',
        ];
        $fields = array_combine($fields, $fields);
        return $fields;
    }

    public static function getByName($name) {
        $model = static::findOne(['name'=>$name]);
        if(!$model) {
            $model = new Member();
            $model->name = $name;
            $model->status = true;
            $model->commission = 0;
            $model->salary = 0;
            $model->deposit = $model->commission + $model->salary;
        }
        return $model;
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'user_id'])->inverseOf('members');
    }

    public function getOrders() {
        return $this->hasMany(Order::className(), ['provider_member_id' => 'id'])->inverseOf('providerMember');
    }

    public function getOrders0() {
        return $this->hasMany(Order::className(), ['trader_member_id' => 'id'])->inverseOf('traderMember');
    }

    public function getProducts() {
        return $this->hasMany(Product::className(), ['designer_id' => 'id'])->inverseOf('designer');
    }
}
